package com.shiguiwu.springmybatis.designpattern.observe.service;

import com.shiguiwu.springmybatis.designpattern.observe.LotteryResult;
import com.shiguiwu.springmybatis.designpattern.observe.event.EventManager;
import com.shiguiwu.springmybatis.designpattern.observe.event.listener.MQEventListener;
import com.shiguiwu.springmybatis.designpattern.observe.event.listener.MessageEventListener;

/**
 * @description: fuwu
 * @author: stone
 * @date: Created by 2021/9/3 14:40
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.observe.service
 */
public abstract class LotteryService {


    private EventManager eventManager;

    public LotteryService() {
        eventManager = new EventManager(EventManager.EventType.Message, EventManager.EventType.MQ);
        eventManager.subscribe(EventManager.EventType.MQ,new MQEventListener());
        eventManager.subscribe(EventManager.EventType.Message, new MessageEventListener());

    }

    public LotteryResult draw(String userId) {
        LotteryResult lotteryResult = this.doDraw(userId);
        //发送短信
        eventManager.notify(EventManager.EventType.Message, lotteryResult);

        //发送消息队列
        eventManager.notify(EventManager.EventType.MQ, lotteryResult);

        return lotteryResult;
    }


    public abstract LotteryResult doDraw(String userId);
}
